// 柱状图组件
import * as echarts from "echarts";
import { useEffect, useRef } from "react";

const BarChart = ({ title, DataX, DataY }) => {
  const chartRef = useRef(null);
  useEffect(() => {
    //保证dom加载完毕
    // 1.获取渲染图表的dom节点
    // const chartDom = document.getElementById("main");
    // 初始化生成图表实例对象
    const myChart = echarts.init(chartRef.current);
    const option = {
      title: {
        text: title,
      },
      xAxis: {
        type: "category",
        data: DataX,
      },
      yAxis: {
        type: "value",
      },
      series: [
        {
          data: DataY,
          type: "bar",
        },
      ],
    };
    // 渲染
    option && myChart.setOption(option);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  return <div ref={chartRef} style={{ width: "500px", height: "400px" }}></div>;
};

export default BarChart;
